<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="/static/layui-v2.5.6/layui/css/layui.css" media="all">
  <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>            
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
  <legend>商品添加</legend>
</fieldset>
 
<form class="layui-form" action="">
  <div class="layui-form-item">
    <label class="layui-form-label">商品名称</label>
    <div class="layui-input-block">
      <input type="text" name="name" lay-verify="title" autocomplete="off" placeholder="请输入商品名称" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">商品分类</label>
    <div class="layui-input-block">
      <input type="text" name="classify" lay-verify="required" lay-reqtext="商品名称是必填项，岂能为空？" placeholder="请输入商品分类信息" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item layui-form-text">
    <label class="layui-form-label">商品简介</label>
    <div class="layui-input-block">
      <textarea class="layui-textarea layui-hide" name="sign" lay-verify="content" id="LAY_demo_editor"></textarea>
    </div>
  </div>


  <div class="layui-form-item">
    <label class="layui-form-label">商品售价</label>
    <div class="layui-input-block">
      <input type="text" name="sellprice" lay-verify="required" placeholder="请输入商品售价" autocomplete="off" class="layui-input">
    </div>
  </div>

  <div class="layui-form-item">
    <label class="layui-form-label">商品库存</label>
    <div class="layui-input-block">
      <input type="text" name="trade" lay-verify="required" placeholder="请输入商品库存" autocomplete="off" class="layui-input">
    </div>
  </div>

  <div class="layui-form-item">
    <label class="layui-form-label">销售数量</label>
    <div class="layui-input-block">
      <input type="text" name="salesvolume" lay-verify="required" placeholder="请输入商品销售数量" autocomplete="off" class="layui-input">
    </div>
  </div>

  <div class="layui-form-item">
    <label class="layui-form-label">是否下架</label>
    <div class="layui-input-block">
      <select name="flag" lay-filter="aihao">
        <option value=""></option>
        <option value="0">是</option>
        <option value="1">否</option>
      </select>
    </div>
  </div>

  <div class="layui-form-item">
    <label class="layui-form-label">上架时间</label>
    <div class="layui-input-block">
      <input type="text" name="time" lay-verify="required" placeholder="请输入商品上架时间" autocomplete="off" class="layui-input">
    </div>
  </div>
  
  <button type="button" class="layui-btn" id="test2">多图片上传</button> 
  <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
    预览图：
    <div class="layui-upload-list" id="demo2"></div>
 </blockquote>
</div>
</div>  
    <div class="layui-form-item">
    <div class="layui-input-block">
      <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
      <a class="layui-btn layui-btn-sm" href="/index/product_list.html" target="body_show">返回商品列表</a>
    </div>
  </div>
</form>

        
<script src="/static/layui-v2.5.6/layui/layui.js" charset="UTF-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
layui.use(['form', 'layedit', 'laydate','upload'], function(){
  var form = layui.form
  ,layedit = layui.layedit
  ,laydate = layui.laydate
  ,upload = layui.upload;

  var $ = layui.jquery
  ,layer = layui.layer;
  
  //日期
  laydate.render({
    elem: '#date'
  });
  laydate.render({
    elem: '#date1'
  });
  
  //创建一个编辑器
  var editIndex = layedit.build('LAY_demo_editor');
 
  //自定义验证规则
  form.verify({
    title: function(value){
      if(value.length < 5){
        return '标题至少得5个字符啊';
      }
    }
    ,pass: [
      /^[\S]{6,12}$/
      ,'密码必须6到12位，且不能出现空格'
    ]
    ,content: function(value){
      layedit.sync(editIndex);
    }
  });
  
  //监听指定开关
  form.on('switch(switchTest)', function(data){
    layer.msg('开关checked：'+ (this.checked ? 'true' : 'false'), {
      offset: '6px'
    });
    layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
  });
  
  //监听提交
  form.on('submit(demo1)', function(data){
    layer.alert(JSON.stringify(data.field), {
      title: '最终的提交信息'
    })
    return false;
  });
 
  //表单赋值
 /* layui.$('#LAY-component-form-setval').on('click', function(){
    form.val('example', {
      "username": "贤心" // "name": "value"
      ,"password": "123456"
      ,"interest": 1
      ,"like[write]": true //复选框选中状态
      ,"close": true //开关状态
      ,"sex": "女"
      ,"desc": "我爱 layui"
    });
  });*/
  //上传多个图片
  var uploadInst =upload.render({
    elem: '#test2'
    ,url: 'https://httpbin.org/post' //改成您自己的上传接口
    ,multiple: true
    ,before: function(obj){
      //预读本地文件示例，不支持ie8
      obj.preview(function(index, file, result){
        $('#demo2').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
      });
    }
    ,done: function(res){
      //上传完毕
    }
  });
  
  //表单取值
  layui.$('#LAY-component-form-getval').on('click', function(){
    var data = form.val('example');
    alert(JSON.stringify(data));
  });
  
});
</script>

</body>
</html>